package temp.lho.lws.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import temp.lho.lws.pojo.CarDamageInfo;
import temp.lho.lws.pojo.HumanInjuryInfo;
import temp.lho.lws.pojo.ObjectDamageInfo;
import temp.lho.lws.pojo.VerifyLossInfo;
import temp.lho.lws.result.ResultBody;
import temp.lho.lws.service.ICarDamageInfoService;
import temp.lho.lws.service.IHumanInjuryInfoService;
import temp.lho.lws.service.IObjectDamageInfoService;
import temp.lho.lws.service.IVerifyLossInfoService;

import java.math.BigDecimal;
import java.util.List;

/**
 * (VerifyLossInfo)表控制层
 *
 * @author makejava
 * @since 2020-07-06 23:08:54
 */
@RestController
@RequestMapping("/verifyLossInfo")
public class VerifyLossInfoController {
    /**
     * 服务对象
     */
    @Autowired
    private IVerifyLossInfoService verifyLossInfoService;
    @Autowired
    private ICarDamageInfoService carDamageInfoService;
    @Autowired
    private IHumanInjuryInfoService humanInjuryInfoService;
    @Autowired
    private IObjectDamageInfoService objectDamageInfoService;

    /**
     * 查询核损信息
     * @param claimmainid
     * @return
     */
    @GetMapping("/findByClaimMainId")
    public ResultBody findByClaimMainId(Integer claimmainid) {
        VerifyLossInfo byClaimMainId = verifyLossInfoService.findByClaimMainId(claimmainid);
        return byClaimMainId == null ?
                ResultBody.toResultBody(-1, "")
                :ResultBody.toResultBody(200,"",byClaimMainId);
    }

    /**
     * 计算核损，并且插入
     * @param claimmainid
     * @return
     */
    @GetMapping("/computeVerify")
    public ResultBody computeVerify(Integer claimmainid) {
        //总金额
        BigDecimal sumMoney = new BigDecimal("0.00");

        //查询车损
        List<CarDamageInfo> carInfo = carDamageInfoService.findByClaimmainid(claimmainid);
        //查询物损
        List<ObjectDamageInfo> objectInfo = objectDamageInfoService.findByClaimmainid(claimmainid);
        //查询人伤
        List<HumanInjuryInfo> humanInfo = humanInjuryInfoService.findByClaimmainid(claimmainid);

        //累加计算总额
        for (CarDamageInfo carDamageInfo : carInfo) {
            sumMoney = sumMoney.add(BigDecimal.valueOf(carDamageInfo.getLosstotal()));
        }
        for (ObjectDamageInfo objectDamageInfo : objectInfo) {
            sumMoney = sumMoney.add(BigDecimal.valueOf(objectDamageInfo.getLosstotal()));
        }
        for (HumanInjuryInfo humanInjuryInfo : humanInfo) {
            sumMoney = sumMoney.add(BigDecimal.valueOf(humanInjuryInfo.getLosstotal()));

        }
        //插入核损信息
        VerifyLossInfo verifyLossInfo = new VerifyLossInfo();
        verifyLossInfo.setClaimmainid(claimmainid);
        verifyLossInfo.setVerifylosscost(sumMoney.doubleValue());
        verifyLossInfo.setVerifylossopinion("插入核损信息，赔案id为：" + claimmainid);

        int add = verifyLossInfoService.add(verifyLossInfo);

        return add > 0 ?
                ResultBody.toResultBody(200, "计算金额成功！",verifyLossInfo)
                :ResultBody.toResultBody(200,"计算金额失败！");
    }

}